package 数组;

import java.util.Arrays;

public class LC_977有序数组的平方 {
    public static void main(String[] args){
        int[] nums={-7,-3,2,3,11};
        System.out.println(Arrays.toString(sortedSquares(nums)));
    }
    //两头指针往中间靠拢
    public static int[] sortedSquares(int[] nums) {
        int[] result=new int[nums.length];
        int k=nums.length-1;
        for(int i=0,j=nums.length-1;i<=j;){
            int m=nums[i]*nums[i];
            int n=nums[j]*nums[j];
            if(m>n){
                result[k--]=m;
                i++;
            }
            else{
                result[k--]=n;
                j--;
            }
        }
        return result;
    }
}
